import {requestIsSuccess} from "@/api/request";
import {warningMessage} from "@/utils/message";
import {MyMapOpt} from "@/class/map/myMapOpt";
import {Marker, MarkerData} from "@/class/map/Maker";
import {MapData} from "@/class/map/MapData";
import {getUUID} from "@/utils/utils";

export default {
    namespaced: true,
    state:{
        //编辑或添加的要素
        element:undefined,//MarkerData,PolylineData,PolygonData
        fillOpacitySelect:[
            {
                key:0,
                label:"透明",
                value:"0"
            },
            {
                key:1,
                label:"半透明",
                value:"0.5"
            },
            {
                key:2,
                label:"不透明",
                value:"1"
            },
        ],
        //编辑窗口的可见性
        visible:false,
        //要素的坐标值和路径
        path:null,
        //窗口的类型：新增，编辑，新增编辑（编辑shapefile读取的数据，并新增）
        dialogType:MyMapOpt.DialogType.AddData,
        //窗口现在编辑的要素类型：点线面
        addType:MyMapOpt.MapType.Marker
    },
    mutations:{
        SET_ELEMENT(state, element){
            state.element = element;
        },
        SET_VISIBLE(state, flag){
            state.visible = flag;
        },
        SET_PATH(state, path){
            state.path = path;
        },
        SET_DIALOG_TYPE(state, dialogType){
            state.dialogType = dialogType;
        },
        SET_ADD_TYPE(state, addType){
            state.addType = addType;
        }
    },
    actions:{
        setElement({commit},element){
            let path = element.getElement().path||element.getElement().position
            commit("SET_ADD_TYPE",element.mapData.type)
            commit("SET_PATH", path)
            commit("SET_ELEMENT", element);
        },
        openEdit({commit},dialogType){
            commit("SET_DIALOG_TYPE",dialogType)
            commit("SET_VISIBLE",true)
        },
        closeEdit({commit}){
            commit("SET_VISIBLE",false)
        },
        setPath({commit},path){
            commit("SET_PATH",path);
        },
        /**
         * 关闭修改窗口，并修改数据
         */
        async saveEditDialog({state}) {
            if (state.element.mapData.type === MyMapOpt.MapType.Marker) {
                state.element.getElement().position = state.path
            } else
                state.element.getElement().path = state.path
            if(state.dialogType===MyMapOpt.DialogType.AddData||state.dialogType===MyMapOpt.DialogType.EditAndAddData){
                let result = await state.element.upload();
                if (result) {
                    return result;
                } else {
                    warningMessage("上传失败")
                }
            }else{
                let result = await state.element.update()
                if (result) {
                    return result;
                } else {
                    warningMessage("更新失败")
                }
            }
        },

        async deleteEditDialog({state}){
            let result = await state.element.deleteElement();
            if (requestIsSuccess(result)) {
                return result;
            } else {
                warningMessage("删除失败")
            }
        }
    }
}